package com.flycms.module.role.service;

import com.flycms.core.entity.RestDataVo;
import com.flycms.core.utils.SnowFlake;
import com.flycms.module.role.dao.AdminRoleDao;
import com.flycms.module.role.model.AdminRole;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author huangzt
 * @version 1.0
 * @date 2020/2/24 18:04
 */
@Service
public class AdminRoleService {
    @Autowired
    private AdminRoleDao roleDao;

    public RestDataVo queryRoleByGroup(long groupId){
        List<Map<String, Object>> list = roleDao.queryRoleByGroup(groupId);
        List<AdminRole> adminRoles = roleDao.queryAll();
        List<Map<String, Object>> adminRoleList = adminRoles.stream().map(a->{
            Map<String,Object> map = new HashMap<>(16);
            map.put("role_id",a.getId());
            map.put("urlKey",a.getUrlKey());
            map.put("pid",a.getPid());
            map.put("remark",a.getRemark());
            map.put("status",a.getStatus());
            return map;
        }).collect(Collectors.toList());
        for (int i = 0; i < adminRoleList.size() ; i++) {
            for (int j = 0; j <list.size() ; j++) {
                if(adminRoleList.get(i).get("role_id").toString().equals(list.get(j).get("id").toString())){
                    adminRoleList.get(i).put("isHas",true);
                }else{
                    if(!adminRoleList.get(i).containsKey("isHas")){
                        adminRoleList.get(i).put("isHas",false);
                    }
                }
            }
        }
        return RestDataVo.OK("成功",adminRoleList);
    }

    public RestDataVo queryRoleList(){
        List<AdminRole> adminRoles = roleDao.queryAll();
        List<Map<String, Object>> adminRoleList = adminRoles.stream().map(a->{
            Map<String,Object> map = new HashMap<>(16);
            map.put("pid",a.getPid());
            map.put("remark",a.getRemark());
            map.put("status",a.getStatus());
            map.put("role_id",a.getId());
            map.put("urlKey",a.getUrlKey());
            return map;
        }).collect(Collectors.toList());
        return RestDataVo.OK("成功",adminRoleList);
    }

    public RestDataVo insertRoleGroup(long roleid,long groupid){
        int i = roleDao.insertRoleGroup(roleid, groupid);
        if(i==1){
            return RestDataVo.OK("分配成功");
        }
        return RestDataVo.ERROR("分配失败");
    }

    public RestDataVo deleteRoleGroup(long roleid,long groupid){
        int i = roleDao.deleteRoleGroup(roleid, groupid);
        if(i==1){
            return RestDataVo.OK("取消分配成功");
        }
        return RestDataVo.ERROR("取消分配失败");
    }


    public RestDataVo insertRole(AdminRole role){
        SnowFlake snowFlake = new SnowFlake(2, 3);
        role.setId(snowFlake.nextId());
        int i = roleDao.insert(role);
        if(i==1){
            return RestDataVo.OK("新增成功");
        }
        return RestDataVo.ERROR("新增失败");
    }

    public RestDataVo editRole(AdminRole role){
        int i = roleDao.update(role);
        if(i==1){
            return RestDataVo.OK("修改成功");
        }
        return RestDataVo.ERROR("修改失败");
    }

    public AdminRole queryRoleByPK(String roleId){
        return roleDao.queryByPk(roleId);
    }

}

